﻿using System;
using System.Globalization;
using ContractManagementSystem.EntityObjects;
using ContractManagementSystem.BusinessLogicLayer;

namespace ContractManagementSystem.Presentation.ProfileManagement
{
    public partial class UpdateConsultant : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            CheckUserPermission();
            if (!IsPostBack)
                SelectData();
        }

        // Check user's permissions
        private void CheckUserPermission()
        {
            var userName = CommonLogic.ConvertToString(Session["UserName"]);
            const int functionId = (int)FunctionList.AddConsultant;

            if (Page.Master == null) return;
            var urlToCome = Page.Master.ResolveUrl("Presentation/AuthorizationError.aspx");

            AuthorizationChecker.IsValidAuthorization(userName, functionId, Response, urlToCome);
        }

        protected void SelectData()
        {
            var id = Request.QueryString["id"];
            if (!string.IsNullOrEmpty(id))
            {
                var dataTable = ConsultantProfileLogic.SelectConsultantByConsultantId(id);
                textConsultantID.Text = id;
                textUserName.Text = dataTable.Rows[0]["UserName"].ToString();
                textFullName.Text = dataTable.Rows[0]["FullName"].ToString();
                textEmail.Text = dataTable.Rows[0]["Email"].ToString();
                textCompanyName.Text = dataTable.Rows[0]["CompanyName"].ToString();
                textPhoneNumber.Text = dataTable.Rows[0]["PhoneNumber"].ToString();
                textAddress.Text = dataTable.Rows[0]["Address"].ToString();
            }

        }

        protected void ButtonUpdateConsultant(object sender, EventArgs e)
        {
            var eConsultant = new ConsultantEntity
                                  {
                                      ConsultantId = int.Parse((textConsultantID.Text), CultureInfo.InvariantCulture),
                                      FullName = Server.HtmlEncode(textFullName.Text),
                                      Email = Server.HtmlEncode(textEmail.Text),
                                      CompanyName = Server.HtmlEncode(textCompanyName.Text),
                                      PhoneNumber = Server.HtmlEncode(textPhoneNumber.Text),
                                      Address = Server.HtmlEncode(textAddress.Text)
                                  };
            var result = ConsultantProfileLogic.UpdateConsultants(eConsultant);

            if (result != null)
            {
                Response.Redirect("ListConsultant.aspx");
            }
            else
            {
                var userEventEntity = new EventEntity
                {
                    UserName = CommonLogic.ConvertToString(Session["UserName"]),
                    EventType = (int)EventTypeList.UserEvent,
                    OnDate = DateTime.Now,
                    Description = "User update failed"
                };
                EventTracer.SaveEvents(userEventEntity);

            }
        }

        protected void ButtonReset_Click(object sender, EventArgs e)
        {
            textFullName.Text = "";
            textEmail.Text = "";
            textCompanyName.Text = "";
            textPhoneNumber.Text = "";
            textAddress.Text = "";
        }
    }
}
